package com.ext.aic.apis.gc;

import android.content.Context;

import androidx.lifecycle.LifecycleOwner;

import com.ext.aic.apis.ApiDefine;
import com.ext.aic.apis.ApiPaths;
import com.ext.aic.builder.ParamsBuilder;
import com.ext.aic.apis.RequestResponse;
import com.ext.aic.beans.CompanyItem;
import com.ext.aic.beans.Pagination;
import com.ext.aic.beans.response.PageResponse;
import com.ext.aic.utils.ToastyUtils;
import com.hjq.http.EasyHttp;

import java.util.List;

public class CompanyListRequest {

    protected void onCompanyListSuccess(List<CompanyItem> companyItems, Pagination pagination) {

    }

    public void request(Context context, LifecycleOwner lifecycleOwner, int page, int size, String industryId, String keyword) {
        ParamsBuilder builder = new ParamsBuilder();
        builder.add("l2_id", industryId);
        builder.add("keyword", keyword);
        builder.add("page", page);
        builder.add("page_size", size);
        ApiDefine apiDefine = new ApiDefine(ApiPaths.companyList, builder);
        EasyHttp.get(lifecycleOwner).api(apiDefine).request(new RequestResponse<PageResponse<List<CompanyItem>>>(context, true) {
            @Override
            protected void onSuccess(PageResponse<List<CompanyItem>> response) {
                if (response.getCode() == 200) {
                    Pagination pagination = new Pagination();
                    pagination.setPage(response.getPage());
                    pagination.setPageSize(response.getPageSize());
                    pagination.setTotal(response.getTotal());
                    onCompanyListSuccess(response.getData(), pagination);
                } else {
                    ToastyUtils.getInstance().show(response.getMessage());
                }
            }
        });
    }
}
